//
//  XHomeItemContentCell.m
//  Bees
//
//  Created by Xiaohan on 2018/12/18.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XHomeItemContentCell.h"
#import "XHomeItemCell.h"
#import "FoundModel.h"
@interface XHomeItemContentCell()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
@property (nonatomic,strong) UICollectionView*collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout*flowLayout;

@end

@implementation XHomeItemContentCell

- (void)setSelIndex:(NSInteger)selIndex{
    if (_selIndex != selIndex) {
        _selIndex = selIndex;
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_selIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    }
}
//- (void)selectItemNoti:(NSNotification*)noti{
//    NSInteger selIndex = [noti.object integerValue];
//    if (_selIndex != selIndex) {
//        _selIndex = selIndex;
//        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_selIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
//    }
//}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setCategoryData:(NSArray<FoundCategoryModel *> *)categoryData{
    _categoryData = categoryData;
    [self.collectionView reloadData];
}
- (void)setupUI{
//    [KPostNotificationCenter addObserver:self selector:@selector(selectItemNoti:) name:kXHomePageSegementChangeNoti object:nil];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.bottom.mas_equalTo(0);
    }];
}
#pragma mark - delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   return self.categoryData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XHomeItemContentClvCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XHomeItemContentClvCell class]) forIndexPath:indexPath];
    kWeakSelf(self);
    cell.MoreBlock = ^() {
        !weakself.MoreBtnClickBlock?:weakself.MoreBtnClickBlock([weakself.categoryData objectAtIndex:weakself.selIndex]);
    };
    cell.ItemClickBlock = ^(FoundRecommendModel *model) {
        !weakself.ItemClickBlock?:weakself.ItemClickBlock(model);
    };

    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    XHomeItemContentClvCell *cells =(XHomeItemContentClvCell *)cell;
    FoundCategoryModel*category = [self.categoryData objectAtIndex:indexPath.item];
    cells.data = category.list;
}
#pragma mark - scroll

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offset = scrollView.contentOffset.x;
    NSInteger currentIndex = offset/self.flowLayout.itemSize.width;
    if (_selIndex != currentIndex) {
        _selIndex = currentIndex;
        !self.scrollToIndexBlock?:self.scrollToIndexBlock(_selIndex);
//        KPostNotification(kXHomePageScrollOffsetChangeNoti, @(_selIndex));
    }
}

#pragma mark - getter
- (UICollectionView*)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[XHomeItemContentClvCell class] forCellWithReuseIdentifier:NSStringFromClass([XHomeItemContentClvCell class])];
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}
- (UICollectionViewFlowLayout*)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [UICollectionViewFlowLayout new];
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout.itemSize = CGSizeMake(kScreenWidth, kXHomeItemContentHeight);
        _flowLayout.minimumLineSpacing=0.0f;
        _flowLayout.minimumInteritemSpacing= 0.0f;
    }
    return _flowLayout;
}
- (void)dealloc{
//    [KPostNotificationCenter removeObserver:self];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@interface XHomeItemContentClvCell()
@property (nonatomic,strong) UIButton*moreBtn;
@end

@implementation XHomeItemContentClvCell

- (void)setData:(NSArray <FoundRecommendModel*>*)data{
    _data = data;
    [self.table reloadData];
    [self layoutSubviews];
}

- (void)setupUI{
    ViewRadius(self.backView, AdaptedWidth(13.0f));
    [self.contentView addSubview:self.backView];
    [self.backView addSubview:self.table];
    [self.backView addSubview:self.moreBtn];
}
- (void)layoutSubviews{
    self.backView.frame = CGRectMake(kNormalSpace, AdaptedWidth(5), kScreenWidth-2*kNormalSpace, self.data.count*kXHomeItemCellHeight+30.0f);
    self.table.frame = CGRectMake(0, kNormalSpace*.5, self.backView.width, self.backView.height-kNormalSpace);
    self.moreBtn.frame = CGRectMake(self.backView.right-70.0f, self.backView.height-30.0f, 50.0f, 30.0f);
    [self.moreBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:1];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XHomeItemCell *cell = [XHomeItemCell cellForTableView:tableView];
    FoundRecommendModel*m = [self.data objectAtIndex:indexPath.row];
    [cell.imageV sd_setImageWithURL:String_To_URL(m.logo) placeholderImage:nil];
    cell.titleLab.text = m.name;
    cell.subLab.text = m.introduction;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    !self.ItemClickBlock?:self.ItemClickBlock([self.data objectAtIndex:indexPath.row]);
}

- (UIView*)backView{
    if (!_backView) {
        _backView = [UIView new];
        _backView.backgroundColor = UIColorHex(#F7F7F7);
    }
    return _backView;
}

- (UITableView*)table{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.rowHeight = kXHomeItemCellHeight;
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.scrollEnabled = NO;
        _table.backgroundColor = UIColor.clearColor;
    }
    return _table;
}
- (UIButton*)moreBtn{
    if (!_moreBtn) {
        _moreBtn = [UIButton new];
        _moreBtn.titleLabel.font = kDefaultFont(12);
        [_moreBtn setTitle:@"更多" forState:UIControlStateNormal];
        [_moreBtn setImage:IMAGE_NAMED(@"gray_arrow") forState:UIControlStateNormal];
        [_moreBtn setTitleColor:UIColorHex(#B5B5B5) forState:UIControlStateNormal];
        kWeakSelf(self);
        [_moreBtn addTapBlock:^(UIButton * _Nonnull btn) {
            !weakself.MoreBlock?:weakself.MoreBlock();
        }];
    }
    return _moreBtn;
}
@end
